package com.dxtx.widget.trackball;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by user on 2016/12/12.
 * 画笔轨迹,轨迹跟随一个渐隐的白线,和一条持续的红线
 */
public class PaintTrackView extends View {
    Path redPath = new Path();
    Path whitePath = new Path();
    Paint redPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Paint whitePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Paint roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    List<PointF> whitePointList = new ArrayList<>();

    private PointF lastP;

    private float lineWidth = 16;

    public PaintTrackView(Context context) {
        super(context);
        init();
    }

    public PaintTrackView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        redPaint.setStyle(Paint.Style.STROKE);
        redPaint.setColor(Color.RED);
        redPaint.setPathEffect(new CornerPathEffect(5));
        redPaint.setStrokeWidth(lineWidth);

        whitePaint.setStyle(Paint.Style.STROKE);
        whitePaint.setColor(0xffffffff);
        whitePaint.setStrokeWidth(lineWidth * 1.5f);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (redPath != null) {
            canvas.drawPath(redPath, redPaint);
        }
        synchronized (whitePointList) {
            if (whitePointList.isEmpty()) {
                if (lastP != null) {
                    roundPaint.setColor(Color.RED);
                    canvas.drawCircle(lastP.x, lastP.y, lineWidth * 0.5f, roundPaint);
                }
            } else {
                drawWhite(canvas);
            }
        }
    }

    private void drawWhite(Canvas canvas) {
        int size = whitePointList.size();
        if (size == 0) {
            return;
        } else if (size < 2) {
            PointF p = whitePointList.get(0);
            roundPaint.setColor(Color.WHITE);
            canvas.drawCircle(p.x, p.y, lineWidth * 0.75f, roundPaint);
            return;
        }
        whitePath.reset();
        PointF p1 = whitePointList.get(0);
        whitePath.moveTo(p1.x, p1.y);
        float div = 1f / (size - 1);
        for (int i = 1; i < size; i++) {
            PointF p = whitePointList.get(i);
            whitePaint.setAlpha((int) (255f * div * i));
            canvas.drawLine(p1.x, p1.y, p.x, p.y, whitePaint);
            p1 = p;
        }
        roundPaint.setColor(Color.WHITE);
        canvas.drawCircle(p1.x, p1.y, lineWidth * 0.75f, roundPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                redPath.reset();
                redPath.moveTo(e.getX(0), e.getY(0));
                synchronized (whitePointList) {
                    whitePointList.clear();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                redPath.lineTo(e.getX(0), e.getY(0));
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                redPath.lineTo(e.getX(0), e.getY(0));
                final Timer timer = new Timer();
                final TimerTask task = new TimerTask() {
                    @Override
                    public void run() {
                        synchronized (whitePointList) {
                            if (whitePointList.isEmpty()) {
                                cancel();
                                timer.cancel();
                                return;
                            } else if (whitePointList.size() == 1) {
                                lastP = whitePointList.remove(0);
                            } else {
                                whitePointList.remove(0);
                            }
                        }
                        postInvalidate();
                    }
                };

                timer.schedule(task, 0, 25);
                return true;
        }
        synchronized (whitePointList) {
            whitePointList.add(new PointF(e.getX(0), e.getY(0)));
            if (whitePointList.size() > 10) {
                whitePointList.remove(0);
            }
        }
        invalidate();
        return true;
    }
}
